unit ORM.Types.CEP;

interface

type
  TCEP = record
  private
    FValue : string;
    FValorReferencia : IInterface;
    function GetValue : string;

    function FormataCEP(pValue: string): string;
    constructor Create(const pValue : string);
  public
    property Value : string read GetValue;

    function IsEmpty : Boolean;
    function GetFormatValue : string;
    procedure Clear;

    class operator Implicit(const pValue : string) : TCEP;
    class operator Implicit(const pValue : TCEP) : string;
  end;

implementation

uses
  ORM.Types.Base, System.SysUtils, ORM.Utils;

{ TCEP }

procedure TCEP.Clear;
begin
  FValue := EmptyStr;
  FValorReferencia := EmptyStr;
end;

constructor TCEP.Create(const pValue: string);
begin
  FValue := SomenteNumeros(pValue);
  SetFlagInterface(CValorReferencia);
end;

class operator TCEP.Implicit(const pValue: TCEP): string;
begin
  Result := pValue.Value;
end;

function TCEP.FormataCEP(pValue: string): string;
begin
  Result := Copy(pValue, 0, 5) + '-' + Copy(pValue, 7, 3);
end;

function TCEP.GetFormatValue: string;
begin
  Result := FormataCEP(FValue)
end;

function TCEP.GetValue: string;
begin
  Result := FValue;
end;

class operator TCEP.Implicit(const pValue: string): TCEP;
begin
  Result := TCEP.Create(pValue);
end;

function TCEP.IsEmpty: Boolean;
begin
  Result := Length(FValorReferencia) = 0;
end;

end.
